#!/bin/bash/python
# _*_ coding: utf-8 _*_
# file： gen_csd_l2mc
# date-time: 2023/11/17-10:55
# auth:hard work
# description
import random
import xml.etree.ElementTree as ET

from test_app_main.test_cases.case_02_l2mc import mcastMac

# dstMac = "01-0c-cd-01-01-02"
#     appid = 3001
#     vlanid = '000'
#     portbitmap = '1,3,4'
#     root = ET.Element('root')
#     tree = ET.ElementTree(root)
#     ET.SubElement(root,'fibConfigList')
#     for port in range(1,21):
#         port1 = random.randint(1, 20)
#         pass
#     m_dstMac = mcastMac(dstMac)
#     for _mMac in m_dstMac:
#
#         print(_mMac)
#         dmac_dom =  ET.Element('dmac')
#         dmac_dom.text = _mMac
#         appid_dom = ET.Element('appid')
#         appid_dom.text = str(appid)
#         vlanid_dom = ET.Element('vlanid')
#         vlanid_dom.text = vlanid
#         portbitmap_dom = ET.Element('portbitmap')
#         portbitmap_dom.text = portbitmap
#
#         # root.append(ET.Element('fibConfigList'))
#         root.append(dmac_dom)
#         root.append(appid_dom)
#         root.append(vlanid_dom)
#         root.append(portbitmap_dom)
#         # root.append(ET.Element('fibConfigList'))
#         __indent(root)
#         tree.write('def.xml', encoding='utf-8', xml_declaration=True)
#         appid += 1

# 增加换行符


class Generte_csd:
    def __init__(self,appid,portbitmap,xml_saveToPath):
        self.appid = appid
        self.portbitmap = portbitmap
        self.xml_save = xml_saveToPath



    def __indent(self,elem, level=0):
        i = "\n" + level * "\t"
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "\t"
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                self.__indent(elem, level + 1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i
    def goose_csd(self,gooseMac):
        """01-0c-cd-01-01-02"""
        root = ET.Element('root')  # 创建根元素
        m_dstMac = mcastMac(gooseMac)
        for _mMac in m_dstMac:

            fibConfigList = ET.SubElement(root, 'fibConfigList')  # 创建子元素并添加属性
            dmac_dom = ET.SubElement(fibConfigList, 'dmac')  # 创建子元素
            dmac_dom.text = _mMac  # 设置子元素文本
            appid_dom = ET.SubElement(fibConfigList, 'appid')
            appid_dom.text = str(self.appid)
            portbitmap_dom = ET.SubElement(fibConfigList, 'portbitmap')
            portbitmap_dom.text = self.portbitmap
            self.__indent(root)
            tree = ET.ElementTree(root)  # 创建 ElementTree 对象
            tree.write(f'tsn_{self.xml_save}_configure.csd')  # 写入 XML 文件
            self.appid += 1
            if len(m_dstMac) == 256:
                break

    def sv_csd(self,svMac):
        """'010c.cd04.0102'"""
        svMac_lst = []
        root = ET.Element('root')  # 创建根元素
        m_dstMac = svMac.replace(svMac[10:14],'{}')
        print(m_dstMac)
        for i in range(258, 514):
            sv_mac = m_dstMac.format("0" + hex(int(i))[2:])
            svMac_lst.append(sv_mac)
        for _mMac in svMac_lst:
            fibConfigList = ET.SubElement(root, 'fibConfigList')  # 创建子元素并添加属性
            dmac_dom = ET.SubElement(fibConfigList, 'dmac')  # 创建子元素
            dmac_dom.text = _mMac  # 设置子元素文本
            appid_dom = ET.SubElement(fibConfigList, 'appid')
            appid_dom.text = str(self.appid)
            portbitmap_dom = ET.SubElement(fibConfigList, 'portbitmap')
            portbitmap_dom.text = self.portbitmap
            self.__indent(root)
            tree = ET.ElementTree(root)  # 创建 ElementTree 对象
            tree.write(f'tsn_{self.xml_save}_configure.csd')  # 写入 XML 文件
            self.appid += 1

    def write_xml(self):
        pass
if __name__ == '__main__':
    appid = 3001
    portbitmap = '17,18,19'
    version= 'v2.26'
    gooseMac = "01-0c-cd-01-01-02"
    svMac = '010c.cd04.0102'
    gen = Generte_csd(appid, portbitmap, version)
    gen.sv_csd(svMac)

    # generate_csd(versin)